package com.cindata

import grails.transaction.Transactional

@Transactional
class CollateralFlowStageRoleService
{
    def initFlowStageRole(Collateral collateral, Workflow workflow)
    {
        def workflowStages = WorkflowStage.findAllByWorkflow(workflow)
        for (
            workflowStage in
                workflowStages)
        {
            workflowStage?.roles?.each {
                def collateralFlowStageRole = new CollateralFlowStageRole()
                collateralFlowStageRole.role = it?.role
                collateralFlowStageRole.teamRole = it?.teamRole
                collateralFlowStageRole.flowStage = CollateralFlowStage.findByCollateralAndStage(collateral, workflowStage?.stage)
                if (collateralFlowStageRole.validate())
                {
                    collateralFlowStageRole.save flush: true
                }
                else
                {
                    println collateralFlowStageRole.errors
                }
            }
        }
    }
}